package com.lancoo.answer.ui.answerSheet;

import android.util.Log;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.lancoo.answer.model.entity.AnswerSheetBean;
import com.lancoo.answer.model.eventBean.AnswerSheetEventBean;
import com.lancoo.answer.ui.answerSheet.vh.GeneralTestVH;
import com.lancoo.answer.ui.answerSheet.vh.GradeTestVH;
import com.lancoo.answer.ui.answerSheet.vh.PracticeVH;

import org.greenrobot.eventbus.EventBus;

import java.util.List;
import java.util.PrimitiveIterator;

/**
 * Created by pengmi on 2021/12/3 11:43
 * Description:
 */
public class AnswerSheetAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private final int trainSence;
    private int enableShowAnswerScore=0;//是否显示得分，默认不显示 (0-隐藏，1-显示)

    private OnClickAnswerSheetCallBack onCallBack;

    private final List<AnswerSheetBean> list;
    public AnswerSheetAdapter(List<AnswerSheetBean> list,int trainSence) {
        this.list = list;
        this.trainSence=trainSence;
    }

    public void setEnableShowAnswerScore(int enableShowAnswerScore){
        this.enableShowAnswerScore=enableShowAnswerScore;
    }

    public void setOnCallBack(OnClickAnswerSheetCallBack onCallBack) {
        this.onCallBack = onCallBack;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (trainSence){
            case 1:
                return GeneralTestVH.getGeneralTestVH(parent,enableShowAnswerScore);
            case 2:
            case 3:
                return GradeTestVH.getGradeTestVH(parent,enableShowAnswerScore);
            default:
                return PracticeVH.getPracticeVH(parent);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        switch (trainSence){
            case 1:
                GeneralTestVH generalTestVH= (GeneralTestVH) holder;
                generalTestVH.initData(position,list.get(position));
                break;
            case 2:
            case 3:
                GradeTestVH gradeTestVH= (GradeTestVH) holder;
                gradeTestVH.initData(position,list.get(position));
                break;
            default:
                PracticeVH practiceVH= (PracticeVH) holder;
                practiceVH.initData(position,list.get(position));
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void onAnswerSheetItemClick(AnswerSheetEventBean bean){
        Log.e("eee","点击了"+new Gson().toJson(bean));
        if(onCallBack!=null){
            onCallBack.onClickAnswerSheet(bean);
        }else{
            EventBus.getDefault().post(bean);
        }

    }
}
